Conditions | 1 |
Paths | 4 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
32 | $.fn.symphonyPickable = function(options) { |
||
33 | var objects = $(this), |
||
34 | settings = { |
||
35 | content: '#contents', |
||
36 | pickables: '.pickable' |
||
37 | }; |
||
38 | |||
39 | $.extend(settings, options); |
||
40 | |||
41 | /*------------------------------------------------------------------------- |
||
42 | Events |
||
43 | -------------------------------------------------------------------------*/ |
||
44 | |||
45 | // Switch content |
||
46 | objects.on('change.pickable', function pick() { |
||
47 | var object = $(this), |
||
48 | choice = object.val(), |
||
49 | relation = object.attr('id') || object.attr('name'), |
||
50 | related = $(settings.pickables + '[data-relation="' + relation + '"]'), |
||
51 | picked, request; |
||
52 | |||
53 | // Handle numeric values |
||
54 | if($.isNumeric(choice) === true) { |
||
55 | choice = 'choice' + choice; |
||
56 | } |
||
57 | |||
58 | // Hide all choices |
||
59 | object.trigger('pickstart.pickable'); |
||
60 | related.hide().find('input, select, textarea').prop('readonly', true); |
||
61 | |||
62 | // Selection found |
||
63 | picked = $('#' + choice); |
||
64 | if(picked.length > 0) { |
||
65 | picked.show().find('input, select, textarea').prop('readonly', false).trigger('pick.pickable'); |
||
66 | object.trigger('pickstop.pickable'); |
||
67 | } |
||
68 | |||
69 | // Selection not found |
||
70 | else { |
||
71 | request = object.data('request'); |
||
72 | |||
73 | // Fetch picked element |
||
74 | if(request) { |
||
75 | $.ajax({ |
||
76 | type: 'GET', |
||
77 | url: request, |
||
78 | data: { 'choice': choice }, |
||
79 | dataType: 'html', |
||
80 | success: function(remote) { |
||
81 | content.append(remote); |
||
82 | remote.trigger('pick.pickable'); |
||
83 | object.trigger('pickstop.pickable'); |
||
84 | } |
||
85 | }); |
||
86 | } |
||
87 | } |
||
88 | }); |
||
89 | |||
90 | /*------------------------------------------------------------------------- |
||
91 | Initialisation |
||
92 | -------------------------------------------------------------------------*/ |
||
93 | |||
94 | var content = $(settings.content); |
||
95 | |||
96 | // Set up relationships |
||
97 | objects.each(function init() { |
||
98 | var object = $(this), |
||
99 | relation = object.attr('id') || object.attr('name'); |
||
100 | |||
101 | object.find('option').each(function() { |
||
102 | $('#' + $(this).val()).attr('data-relation', relation); |
||
103 | }); |
||
104 | }); |
||
105 | |||
106 | // Show picked content |
||
107 | objects.trigger('change.pickable'); |
||
108 | |||
109 | /*-----------------------------------------------------------------------*/ |
||
110 | |||
111 | return objects; |
||
112 | }; |
||
113 | |||
115 |